package personal.hrsw.mybatisplusdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import personal.hrsw.mybatisplusdemo.domain.Student;
import personal.hrsw.mybatisplusdemo.service.StudentService;

import java.util.List;

@Controller
@RequestMapping("/Student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @RequestMapping("list")
    public String list(Model model) {
        List<Student> students=studentService.list();
        model.addAttribute("students", students);
        return "listStudents";
    }

    @RequestMapping("remove")
    public String remove(int id) {
        studentService.remove(id);
        return "redirect:/Student/list/1/3";
    }
    @RequestMapping("toPage")
    public String toPage(String page){
        return page;
    }
//    //登录验证
////    @RequestMapping("loginValidate")
////    public String loginValidate(Student student, HttpServletRequest request, Model model, HttpSession session, HttpServletResponse response){
//////        System.out.println(student.getName()+" , " + student.getPassword()+",from "+request.getRemoteAddr());
////        //controller,MVC,Model,View,Controller,渲染,将username和password变量数据存到model对象中
////   /*     model.addAttribute("username", username);
////        model.addAttribute("password", password);*/
////        //设置一个会话有效的变量，名字是username,值是student.getUsername()
//////        session.setAttribute("username", student.getName());
////        model.addAttribute("student", student);
////        //传list类型的数据
////        List students = new ArrayList();
////        students.add(student);
////        //   students.add(new Student("tom","1",1));
////        //   students.add(new Student("jerry","2",2));
////        model.addAttribute("students", students);
////        //服务器往客户端发送了两对cookie
//////        response.addCookie(new Cookie("username", student.getName()));
//////        response.addCookie(new Cookie("password", student.getPassword()));
////        return "list";
////    }
    @RequestMapping("add")
    public String add(Student student) {
        studentService.add(student);
        return "redirect:/Student/list/1/3";
    }
    @RequestMapping("toEdit")
    public String toEdit(int id, Model model) {
        Student oldStudent=studentService.findById(id);
        model.addAttribute("student", oldStudent);
        return "editStudent";
    }

    @RequestMapping("edit")
    public String edit(Student student) {
        studentService.edit(student);
        return "redirect:/Student/list/1/3";
    }

    @RequestMapping("list/{currPage}/{pageSize}")
//    //分页显示
    public String list(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize")int pageSize){
        if(currPage<1) currPage=1;
        if (pageSize <=0) pageSize=5;
        //List<Student> students = studentService.findByPage(currPage, pageSize);
        Page<Student> page=new Page<>(currPage,pageSize);
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        List<Student> students=studentService.page(page,queryWrapper).getRecords();
        model.addAttribute("students", students);
        return "listStudents";
    }
//    //变量的生命期有4种：page(默认),request,session(会话,anywhere),application(应用，点击量)
    @RequestMapping("loginValidate")
    public String loginValidate(String username, String password, Model model, HttpSession session) {
        if (studentService.login(username, password)) {
            session.setAttribute("username", username);
            return "redirect:/Student/list/1/3";
        } else {
            return "redirect:/Student/login";
        }
    }
    @RequestMapping("login")
    public String login(){
        return "login";
    }
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "login";
    }
}
